<?php

namespace Member\Controller;

/**
 * Description of LoginController
 *
 * @since 1.0 <2015-10-20> SoChishun <14507247@qq.com> Added.
 */
class Login extends \Think\Controller {

    public function index() {
        $m_site = new \XWAM\Model\XwamConfModel();
        $data = $m_site->findSite(1, 'admin');
        $this->assign('site', $data);
        $this->display();
    }

    public function loginSave($login_name = '', $password = '', $captcha = '') {
        if (!\XWAM\Model\SessionFilterModel::getLoginStatus()) {
            exit('您已经出错5次, 请5分钟后再登录!');
        }
        if (!isset($_POST['captcha'])) {
            $captcha = null;
        }
        if (!$login_name || !$password || (!is_null($captcha) && !$captcha)) {
            $this->error('表单填写不完整!');
        }
        $mLogin = new \XWAM\Model\LoginModel();
        $result = $mLogin->login($login_name, $password, $captcha, $msg);
        if ($result) {
            // read site_info
            $m_site = new \XWAM\Model\XwamConfModel();
            $data = $m_site->findSite(1, $msg['user_name'], false);
            if (!$data) {
                exit('未开通CRM功能!');
            }
            session('site_conf', $data);
            // redirect
            redirect(U('Index/index'));
        } else {
            $this->error(xsfn_message_format($msg), '', 1);
        }
    }

    public function logout() {
        \XWAM\Model\LoginModel::logout();
        header('location:' . U('Login/index'));
    }

}
